import requests
from lxml import etree


# 定义一个获取数据并处理的函数
def fetch_data(url):
    headers = {
        'pragma': 'no-cache',
        'accept-language': 'zh-CN,zh;q=0.9',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
        'accept': '*/*',
        'cache-control': 'no-cache',
        'authority': 'www.dongchedi.com',
        'referer': 'https://www.dongchedi.com/auto/series/1234',
    }

    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # 检查请求是否成功
        tree = etree.HTML(response.text)
        return tree
    except requests.exceptions.RequestException as e:
        print(f"请求出错: {e}")
        return None


# 提取数据
def extract_data(tree):
    data = {}

    # 提取各项数据并防止None类型
    data['official_price'] = tree.xpath('//div[@data-row-anchor="official_price"]//div/text()') or [None]
    data['sub_brand_name'] = tree.xpath('//div[@data-row-anchor="sub_brand_name"]//div/text()') or [None]
    data['jb'] = tree.xpath('//div[@data-row-anchor="jb"]//div/text()') or [None]
    data['fuel_form'] = tree.xpath('//div[@data-row-anchor="fuel_form"]//div/text()') or [None]
    data['market_time'] = tree.xpath('//div[@data-row-anchor="market_time"]//div/text()') or [None]
    data['engine_description'] = tree.xpath('//div[@data-row-anchor="engine_description"]//div/text()') or [None]
    data['energy_elect_max_power'] = tree.xpath('//div[@data-row-anchor="energy_elect_max_power"]//div/text()') or [
        None]
    data['energy_elect_max_torque'] = tree.xpath('//div[@data-row-anchor="energy_elect_max_torque"]//div/text()') or [
        None]
    data['gearbox_description'] = tree.xpath('//div[@data-row-anchor="gearbox_description"]//div/text()') or [None]
    data['length_width_height'] = tree.xpath('//div[@data-row-anchor="length_width_height"]//div/text()') or [None]
    data['car_body_struct'] = tree.xpath('//div[@data-row-anchor="car_body_struct"]//div/text()') or [None]
    data['max_speed'] = tree.xpath('//div[@data-row-anchor="max_speed"]//div/text()') or [None]
    data['acceleration_time'] = tree.xpath('//div[@data-row-anchor="acceleration_time"]//div/text()') or [None]
    data['wltc_fuel_comprehensive'] = tree.xpath('//div[@data-row-anchor="wltc_fuel_comprehensive"]//div/text()') or [
        None]
    data['period'] = tree.xpath('//div[@data-row-anchor="period"]//div/text()') or [None]
    data['maintain_cost'] = tree.xpath('//div[@data-row-anchor="maintain_cost"]//div/text()') or [None]

    return data


# 打印有效数据
def print_valid_data(data):
    # 确保所有列表的长度一致
    max_length = max(len(val) for val in data.values())

    # 补充缺失的数据
    for key in data:
        data[key] += [None] * (max_length - len(data[key]))

    # 输出有效数据
    for i in range(max_length):
        row = [data[key][i] for key in data]

        # 过滤掉只有None的无效数据
        if all(val is None for val in row):
            continue

        print("\t".join([str(val) if val is not None else "N/A" for val in row]))  # 输出数据，N/A替代None


def main():
    for i in range(398, 399):  # 遍历需要抓取的车系ID范围
        url = f'https://www.dongchedi.com/auto/params-carIds-x-{i}'
        tree = fetch_data(url)

        if tree is None:
            continue  # 如果页面抓取失败，跳过当前循环

        # 提取数据
        data = extract_data(tree)

        # 打印有效数据
        print_valid_data(data)


if __name__ == "__main__":
    main()
